<template>
  <el-card shadow="always">
    <div slot="header">
      <h2>用户注册</h2>
      <el-button
        type="info"
        icon="el-icon-back"
        size="mini"
        @click="$router.push({name:'home'})"
        plain
      >返回登录界面</el-button>
    </div>
    <el-form :model="form" ref="form" :rules="rules" status-icon label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input type="password" v-model="form.checkPassword" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="form.phone" style="width:400px"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.gender">
          <el-option label="男" value="m"></el-option>
          <el-option label="女" value="f"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="regist('form')">立即注册</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.checkPassword !== "") {
          this.$refs.form.validateField("checkPassword");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {
        username: "",
        password: "",
        checkPassword: "",
        email: "",
        phone: "",
        gender: "m"
      },
      rules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { validator: validatePass, required: true, trigger: "blur" }
        ],
        checkPassword: [
          { validator: validatePass2, required: true, trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入您的邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "格式错误,格式为xxx@qq/163/sina.com/cn",
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            message: "请输入您的手机号码",
            trigger: "blur"
          },
          {
            pattern: /^1[34578]\d{9}$/, //可以写正则表达式呦呦呦
            message: "号码格式为中国大陆11位手机号",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    regist(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.axios
            .post("users/regist", this.form)
            .then(() => {
              this.$message.success("注册成功!正在跳转登录界面!");
              setTimeout(() => {
                this.$router.push({ name: "home" });
              }, 3300);
            })
            .catch(() => {});
        } else {
          this.$message.error("您的信息未填写完全!");
          return false;
        }
      });
    }
    /*regist() {
      this.axios
        .post("users/regist", this.form)
        .then(() => {
          this.$message.success("注册成功!正在跳转登录界面!");
          setTimeout(() => {
            this.$router.push({ name: "home" });
          }, 3300);
        })
        .catch(() => {});
    }*/
  }
};
</script>
<style scoped>
.el-card {
  width: 600px;
  margin: 0 auto;
}
</style>
